/*
 * Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 #include <stdio.h> // 标准输入输出库
 #include <unistd.h> // 提供了POSIX操作系统API的头文件
 
 #include "ohos_init.h" // OpenHarmony系统初始化相关的头文件
 #include "cmsis_os2.h" // CMSIS RTOS API v2的相关头文件
 #include "hal_bsp_sht20.h" // SHT20传感器HAL层接口头文件
 #include "hal_bsp_pcf8574.h" // PCF8574扩展IO芯片HAL层接口头文件
 
 osThreadId_t Task1_ID; // 定义任务1的线程ID变量
 #define TASK_STACK_SIZE 1024 // 定义任务栈大小为1024字节
 
 // 任务1函数，用于读取SHT20传感器数据并打印温度和湿度
 void Task1(void)
 {
     float temperature = 0, humidity = 0; // 定义浮点型变量temperature和humidity，用于存储温度和湿度值
     while (1) {
         SHT20_ReadData(&temperature, &humidity); // 从SHT20传感器读取温度和湿度数据
         printf("temperature = %.2f      humidity = %.2f\r\n", temperature, humidity); // 打印温度和湿度值
         sleep(1); // 延时1秒
     }
 }
 
 // 初始化和创建任务的静态函数
 static void base_sht20_demo(void)
 {
     printf("Enter base_sht20_demo()!"); // 打印进入函数的信息
 
     PCF8574_Init(); // 初始化PCF8574扩展IO芯片
     SHT20_Init(); // 初始化SHT20传感器
 
     osThreadAttr_t options; // 定义线程属性结构体
     options.name = "thread_1"; // 设置线程名称为"thread_1"
     options.attr_bits = 0; // 初始化线程属性位
     options.cb_mem = NULL; // 回调内存指针设为空
     options.cb_size = 0; // 回调内存大小设为0
     options.stack_mem = NULL; // 线程堆栈内存指针设为空
     options.stack_size = TASK_STACK_SIZE; // 设置线程堆栈大小为1024字节
     options.priority = osPriorityNormal; // 设置线程优先级为正常级别
 
     Task1_ID = osThreadNew((osThreadFunc_t)Task1, NULL, &options); // 创建任务1线程
     if (Task1_ID != NULL) { // 判断线程是否创建成功
         printf("ID = %d, Create Task1_ID is OK!\r\n", Task1_ID); // 如果创建成功，打印线程ID
     }
 }
 
 SYS_RUN(base_sht20_demo); // 注册启动函数base_sht20_demo，在系统初始化时执行